<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="爱肖彤真是太好了"><meta name="copyright" content="爱肖彤真是太好了"><meta name="generator" content="Hexo 5.1.1"><meta name="theme" content="hexo-theme-yun"><title>Go语言基础 | 工藤旧二の博客</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="none" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.19/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_stqaphw3j4.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/task/yun.ico"><link rel="mask-icon" href="/task/yun.ico" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/task/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/task/js/utils.js" as="script"><link rel="preload" href="/task/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/task/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/task/css/hexo-theme-yun.css"><link rel="alternate" href="/task/atom.xml" title="工藤旧二の博客" type="application/atom+xml"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"root":"/task/","title":"工藤旧二の博客","version":"0.9.7","anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/task/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script>(function(){
  var bp = document.createElement('script');
  var curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})();</script><meta name="description" content="一、语言介绍1 简介Go 即Golang，是Google公司2009年11月正式对外公开的一门编程语言。 Go是静态强类型语言，是区别于解析型语言的编译型语言(静态：类型固定 强类型：不同类型不允许直接运算)。 2 特性跨平台的编译型语言 语法接近C语言 管道（channel），切片（slice），并发（routine） 有垃圾回收的机制 支持面向对象（但是go中没有类的概念）和面向过程的编程模式">
<meta property="og:type" content="article">
<meta property="og:title" content="Go语言基础">
<meta property="og:url" content="http://huang_zhao.gitee.io/task/2020/01/09/go/%E5%9F%BA%E7%A1%80/GO%E5%85%A5%E9%97%A8/index.html">
<meta property="og:site_name" content="工藤旧二の博客">
<meta property="og:description" content="一、语言介绍1 简介Go 即Golang，是Google公司2009年11月正式对外公开的一门编程语言。 Go是静态强类型语言，是区别于解析型语言的编译型语言(静态：类型固定 强类型：不同类型不允许直接运算)。 2 特性跨平台的编译型语言 语法接近C语言 管道（channel），切片（slice），并发（routine） 有垃圾回收的机制 支持面向对象（但是go中没有类的概念）和面向过程的编程模式">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-01-09T11:17:57.000Z">
<meta property="article:modified_time" content="2020-09-09T11:17:15.000Z">
<meta property="article:author" content="爱肖彤真是太好了">
<meta property="article:tag" content="go">
<meta property="article:tag" content="基础">
<meta name="twitter:card" content="summary"><script src="/task/js/ui/mode.js"></script><link rel="stylesheet" href="/task/css/prism.css" type="text/css"></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/task/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/task/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/task/about/" title="爱肖彤真是太好了"><img width="96" loading="lazy" src="/task/Yun.png" alt="爱肖彤真是太好了"></a><div class="site-author-name"><a href="/task/about/">爱肖彤真是太好了</a></div><a class="site-name" href="/task/about/site.html">工藤旧二の博客</a><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/task/" title="首页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/task/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">57</span></a></div><div class="site-state-item"><a href="/task/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">2</span></a></div><div class="site-state-item"><a href="/task/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">50</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=552858287" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/15081363" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/task/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%AF%AD%E8%A8%80%E4%BB%8B%E7%BB%8D"><span class="toc-text">一、语言介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="toc-text">1 简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%89%B9%E6%80%A7"><span class="toc-text">2 特性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E7%89%88%E6%9C%AC"><span class="toc-text">3 版本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E4%B8%8B%E8%BD%BD"><span class="toc-text">4 下载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-hello-world"><span class="toc-text">5 hello world</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8F%98%E9%87%8F"><span class="toc-text">二、变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E7%B1%BB%E5%9E%8B"><span class="toc-text">三、类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%B8%B8%E9%87%8F"><span class="toc-text">四、常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%87%BD%E6%95%B0"><span class="toc-text">五、函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8%E5%9F%9F%E5%92%8C%E8%8C%83%E5%9B%B4"><span class="toc-text">六、变量作用域和范围</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%A1%A5%E5%85%85%E8%87%AA%E5%B7%B1%E5%8F%91%E7%8E%B0%E7%9A%84%E5%B0%8F%E7%82%B9"><span class="toc-text">补充自己发现的小点</span></a></li></ol></li></ol></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://huang_zhao.gitee.io/task/task/2020/01/09/go/%E5%9F%BA%E7%A1%80/GO%E5%85%A5%E9%97%A8/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="爱肖彤真是太好了"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="工藤旧二の博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Go语言基础</h1><div class="post-meta"><div class="post-time" style="display:block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="创建时间：2020-01-09 19:17:57" itemprop="dateCreated datePublished" datetime="2020-01-09T19:17:57+08:00">2020-01-09</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="修改时间：2020-09-09 19:17:15" itemprop="dateModified" datetime="2020-09-09T19:17:15+08:00">2020-09-09</time></div><span class="post-count"><span class="post-symbolcount"><span class="post-meta-item-icon" title="本文字数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-file-word-line"></use></svg></span> <span title="本文字数">1.2k</span><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读时长"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-timer-line"></use></svg></span> <span title="阅读时长">5m</span></span></span><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/task/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">技术分享</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/task/tags/go/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">go</span></a><a class="tag" href="/task/tags/%E5%9F%BA%E7%A1%80/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">基础</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h3 id="一、语言介绍"><a href="#一、语言介绍" class="headerlink" title="一、语言介绍"></a>一、语言介绍</h3><h4 id="1-简介"><a href="#1-简介" class="headerlink" title="1 简介"></a>1 简介</h4><p>Go 即Golang，是Google公司2009年11月正式对外公开的一门编程语言。</p>
<p>Go是静态强类型语言，是区别于解析型语言的编译型语言(静态：类型固定 强类型：不同类型不允许直接运算)。</p>
<h4 id="2-特性"><a href="#2-特性" class="headerlink" title="2 特性"></a>2 特性</h4><p>跨平台的编译型语言</p>
<p>语法接近C语言</p>
<p>管道（channel），切片（slice），并发（routine）</p>
<p>有垃圾回收的机制</p>
<p>支持面向对象（但是go中没有类的概念）和面向过程的编程模式</p>
<h4 id="3-版本"><a href="#3-版本" class="headerlink" title="3 版本"></a>3 版本</h4><ul>
<li>2009年11月7日 weekly.2009-11-06 —— 早期的版本</li>
<li>2018年8月24日 go1.11 —— modules和WebAssembly支持。</li>
<li>2020 年 8 月 go 1.15</li>
</ul>
<h4 id="4-下载"><a href="#4-下载" class="headerlink" title="4 下载"></a>4 下载</h4><p>开发环境搭建：<a target="_blank" rel="noopener" href="https://golang.google.cn/dl/%E3%80%82">https://golang.google.cn/dl/。</a></p>
<p>一路下一步，默认会添加到环境变量</p>
<p>cmd窗口输入<code>go version</code>判断是否安装完成</p>
<pre class=" language-go"><code class="language-go"><span class="token comment" spellcheck="true">// 常用命令</span>
<span class="token keyword">go</span> env <span class="token comment" spellcheck="true">// go的环境变量</span>
<span class="token keyword">go</span> build <span class="token comment" spellcheck="true">// 编译成可执行文件</span>
<span class="token keyword">go</span> run <span class="token comment" spellcheck="true">// 编译并执行</span></code></pre>
<p>ide下载：<a target="_blank" rel="noopener" href="https://www.jetbrains.com/go/download/#section=mac">https://www.jetbrains.com/go/download/#section=mac</a></p>
<p>一路下一步，试用30天，再打开一个窗口把jar包拖入，重启软件后输入验证码即可永久试用</p>
<p>创建项目后会默认把go的所有项目都放到用户家目录下的go文件夹下，要运行的项目<strong>必须</strong>在这里，不然无法执行</p>
<h4 id="5-hello-world"><a href="#5-hello-world" class="headerlink" title="5 hello world"></a>5 hello world</h4><pre class=" language-go"><code class="language-go"><span class="token comment" spellcheck="true">// 编译型语言的特性，必须有一个入口，一个运行程序只能有一个入口main</span>
<span class="token keyword">package</span> main

<span class="token comment" spellcheck="true">// 打印函数在fmt包下</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"hello word"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 自带换行和python的一样</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="二、变量"><a href="#二、变量" class="headerlink" title="二、变量"></a>二、变量</h3><p>注意点：</p>
<ul>
<li>变量定义了就必须使用，否则报错</li>
<li>变量类型在定义阶段就决定了，无法改变</li>
<li>变量推荐用驼峰体，文件名推荐用下划线分隔</li>
</ul>
<pre class=" language-go"><code class="language-go"><span class="token comment" spellcheck="true">// 变量的三种定义方式</span>

<span class="token comment" spellcheck="true">// 1 全定义</span>
<span class="token comment" spellcheck="true">// var 变量名 类型 = 值</span>
<span class="token comment" spellcheck="true">//var a int = 123</span>
<span class="token comment" spellcheck="true">//fmt.Println(a)</span>

<span class="token comment" spellcheck="true">// 2 类型推导式</span>
<span class="token comment" spellcheck="true">// 和上面的区别就是不用写类型</span>
<span class="token comment" spellcheck="true">//var a = 123</span>
<span class="token comment" spellcheck="true">//fmt.Println(a)</span>

<span class="token comment" spellcheck="true">// 3 简略型</span>
<span class="token comment" spellcheck="true">// 变量名 := 值</span>
a <span class="token operator">:=</span> <span class="token number">123</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">// 4 声明多个变量</span>
a<span class="token punctuation">,</span>b<span class="token operator">:=</span><span class="token number">123</span><span class="token punctuation">,</span><span class="token number">456</span>

<span class="token comment" spellcheck="true">// 5 按默认值定义</span>
<span class="token keyword">var</span> age <span class="token builtin">int</span> <span class="token comment" spellcheck="true">// int类型默认值为0，string默认值为空字符串</span>

<span class="token comment" spellcheck="true">// 坑</span>
<span class="token comment" spellcheck="true">// 原来在go中，变量不能重复定义，但是在多个同时赋值时，左边只要有一个是未定义的，就可以这样使用</span>
a<span class="token operator">:=</span><span class="token number">123</span>
a<span class="token punctuation">,</span>b<span class="token operator">:=</span><span class="token number">789</span><span class="token punctuation">,</span><span class="token number">444</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span>
</code></pre>
<h3 id="三、类型"><a href="#三、类型" class="headerlink" title="三、类型"></a>三、类型</h3><pre class=" language-go"><code class="language-go">基础数据类型
        数字：
            有符号整形
                <span class="token operator">-</span><span class="token builtin">int</span>：在<span class="token number">32</span>位机器是<span class="token builtin">int32</span>，在<span class="token number">64</span>位机器是<span class="token builtin">int64</span>
                <span class="token operator">-</span><span class="token builtin">int8</span>：表示整数范围是：<span class="token number">8</span>个比特位，<span class="token number">8</span>个bit是1byte ，负数和<span class="token number">0</span>， <span class="token number">2</span>的<span class="token number">7</span>次方<span class="token operator">-</span><span class="token number">1</span> 的范围
                <span class="token operator">-</span><span class="token builtin">int16</span> <span class="token number">2</span>的<span class="token number">15</span>次方减一
                <span class="token operator">-</span><span class="token builtin">int32</span>
                <span class="token operator">-</span><span class="token builtin">int64</span>
            无符号整型
                <span class="token operator">-</span><span class="token builtin">uint8</span>   <span class="token number">2</span>的<span class="token number">8</span>次方减一  定义一个人的年龄
                <span class="token operator">-</span><span class="token builtin">uint16</span>
                <span class="token operator">-</span><span class="token builtin">uint32</span>
                <span class="token operator">-</span><span class="token builtin">uint64</span>

            浮点型（小数），表示小数点后长度多少位
                <span class="token operator">-</span><span class="token builtin">float32</span>
                <span class="token operator">-</span><span class="token builtin">float64</span>
            复数
                <span class="token operator">-</span><span class="token builtin">complex64</span>
                <span class="token operator">-</span><span class="token builtin">complex128</span>
            <span class="token builtin">byte</span>：是<span class="token builtin">int8</span>的别名  单引号包裹
            <span class="token builtin">rune</span>：是<span class="token builtin">int32</span>的别名  单引号包裹
        字符串
            双引号包裹
            反引号包裹  <span class="token string">` `</span> 可以换行


        布尔
            <span class="token builtin">bool</span> <span class="token boolean">true</span> 和 <span class="token boolean">false</span>

        数据类型默认值：
            数字类型是<span class="token number">0</span>
            字符串类型是 空字符串
            布尔类型   <span class="token boolean">false</span></code></pre>
<h3 id="四、常量"><a href="#四、常量" class="headerlink" title="四、常量"></a>四、常量</h3><pre class=" language-go"><code class="language-go"><span class="token comment" spellcheck="true">// 只有两种定义方式，且不能修改</span>

<span class="token comment" spellcheck="true">// 1 </span>
<span class="token keyword">const</span> 常量名 常量类型 <span class="token operator">=</span> 常量值

<span class="token comment" spellcheck="true">// 2</span>
<span class="token keyword">const</span> 常量名 <span class="token operator">=</span> 常量值

<span class="token comment" spellcheck="true">// 3 扩展</span>
<span class="token keyword">const</span> name<span class="token punctuation">,</span>age <span class="token operator">=</span> <span class="token string">"hz"</span><span class="token punctuation">,</span><span class="token number">18</span>

<span class="token keyword">const</span> <span class="token punctuation">(</span>
    name <span class="token operator">=</span> <span class="token string">"hz"</span>
    age <span class="token operator">=</span> <span class="token number">18</span>
<span class="token punctuation">)</span>

<span class="token keyword">const</span> <span class="token punctuation">(</span>
    s1 <span class="token operator">=</span><span class="token boolean">iota</span> <span class="token comment" spellcheck="true">// 从0自增  0</span>
    s2 <span class="token operator">=</span><span class="token boolean">iota</span> <span class="token comment" spellcheck="true">// 1</span>
    s3  <span class="token comment" spellcheck="true">// 2</span>
    s4 <span class="token operator">=</span><span class="token number">99</span>
    s5 <span class="token operator">=</span><span class="token boolean">iota</span> <span class="token comment" spellcheck="true">// 4</span>
<span class="token punctuation">)</span></code></pre>
<h3 id="五、函数"><a href="#五、函数" class="headerlink" title="五、函数"></a>五、函数</h3><pre class=" language-go"><code class="language-go"><span class="token comment" spellcheck="true">// 有参，有返回值函数的调用与定义</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    msg<span class="token punctuation">,</span>s <span class="token operator">:=</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>msg<span class="token punctuation">,</span>s<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">// 多个同类型参数可以省略类型，如果有返回值就必须定义时书写返回值类型，而且写了就必须一一对应返回</span>
<span class="token keyword">func</span> <span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token builtin">string</span><span class="token punctuation">,</span><span class="token builtin">int</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string">"加法"</span><span class="token punctuation">,</span>a<span class="token operator">+</span>b
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// 匿名函数</span>
<span class="token comment" spellcheck="true">// 在go中函数是一等公民，可以把函数的内存地址传给变量</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    a <span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"我是匿名函数"</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token comment" spellcheck="true">// 要遵循变量被定义就必须使用的原则</span>
    <span class="token function">a</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// 函数的参数是函数，函数的返回值也是函数</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    add <span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> a<span class="token operator">+</span>b
    <span class="token punctuation">}</span>
    newAdd <span class="token operator">:=</span> <span class="token function">niuBiDeFunc</span><span class="token punctuation">(</span>add<span class="token punctuation">)</span>
    msg <span class="token operator">:=</span> <span class="token function">newAdd</span><span class="token punctuation">(</span><span class="token string">"你好"</span><span class="token punctuation">,</span><span class="token string">"帅"</span><span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">niuBiDeFunc</span><span class="token punctuation">(</span>add <span class="token keyword">func</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token keyword">func</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">string</span>  <span class="token punctuation">{</span>
    msg <span class="token operator">:=</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"我是"</span><span class="token punctuation">,</span><span class="token string">"hz"</span><span class="token punctuation">)</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
    <span class="token keyword">return</span> add
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// 闭包函数 1 定义在函数内部 2 对外部作用域有引用</span>
<span class="token keyword">func</span> <span class="token function">test</span><span class="token punctuation">(</span>age <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    a<span class="token operator">:=</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> a
<span class="token punctuation">}</span></code></pre>
<h3 id="六、变量作用域和范围"><a href="#六、变量作用域和范围" class="headerlink" title="六、变量作用域和范围"></a>六、变量作用域和范围</h3><pre class=" language-go"><code class="language-go"><span class="token comment" spellcheck="true">// 定义在全局的变量</span>
<span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    <span class="token comment" spellcheck="true">// 拿到的是全局的，改的也是全局的</span>
    age <span class="token operator">=</span> <span class="token number">19</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    age <span class="token operator">=</span> <span class="token number">20</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// 定义在全局和局部的变量</span>
<span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// 在main函数名称空间内用的是自己的age</span>
    age <span class="token operator">:=</span> <span class="token number">0</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    age <span class="token operator">=</span> <span class="token number">19</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
    age <span class="token operator">=</span> <span class="token number">20</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<h5 id="补充自己发现的小点"><a href="#补充自己发现的小点" class="headerlink" title="补充自己发现的小点"></a>补充自己发现的小点</h5><p>go语言在最外层同级别下只能存在关键字</p>
<pre class=" language-go"><code class="language-go"><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment" spellcheck="true">// 报错</span>
hz <span class="token operator">:=</span> <span class="token number">123</span>

<span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span></code></pre>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="打赏" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">点我就给你看点有意思的</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg" alt="QQ 支付" title="QQ 支付"></a><div><span style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg" alt="微信支付" title="微信支付"></a><div><span style="color:#2DC100"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-pay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong>爱肖彤真是太好了</li><li class="post-copyright-link"><strong>本文链接：</strong><a href="http://huang_zhao.gitee.io/task/2020/01/09/go/%E5%9F%BA%E7%A1%80/GO%E5%85%A5%E9%97%A8/" title="Go语言基础">http://huang_zhao.gitee.io/task/2020/01/09/go/%E5%9F%BA%E7%A1%80/GO%E5%85%A5%E9%97%A8/</a></li><li class="post-copyright-license"><strong>版权声明：</strong>本博客所有文章除特别声明外，均默认采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> 许可协议。</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/task/2020/01/10/go/%E5%9F%BA%E7%A1%80/Go%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/" rel="prev" title="Go基础语法"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">Go基础语法</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/task/2019/09/18/%E5%85%B6%E4%BB%96/Docker%E9%AB%98%E7%BA%A7/" rel="next" title="Docker高级"><span class="post-nav-text">Docker高级</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 爱肖彤真是太好了</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v5.1.1</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v0.9.7</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2019-04-12T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="搜索"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/task/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="搜索..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/task/js/utils.js"></script><script defer src="/task/js/hexo-theme-yun.js"></script></body></html>